package com.nanyang.tms.manager.waybill.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.forecast.ForecastDeclareDao;
import com.nanyang.tms.dao.forecast.ForecastReceiverDao;
import com.nanyang.tms.dao.forecast.ForecastSenderDao;
import com.nanyang.tms.dao.forecast.ForecastVolumeDao;
import com.nanyang.tms.dao.receipt.ReceiptRelationDao;
import com.nanyang.tms.dao.waybill.WaybillIncomeFeeDao;
import com.nanyang.tms.dao.waybill.WaybillReceiptDao;
import com.nanyang.tms.domain.forecast.ForecastDeclareVO;
import com.nanyang.tms.domain.forecast.ForecastReceiverVO;
import com.nanyang.tms.domain.forecast.ForecastSenderVO;
import com.nanyang.tms.domain.forecast.ForecastVolumeVO;
import com.nanyang.tms.domain.receipt.ReceiptRelationVO;
import com.nanyang.tms.domain.waybill.WaybillDetailVO;
import com.nanyang.tms.domain.waybill.WaybillReceiptQuery;
import com.nanyang.tms.domain.waybill.WaybillReceiptVO;
import com.nanyang.tms.manager.waybill.WaybillReceiptManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component("waybillReceiptManager")
public class WaybillReceiptManagerImpl implements WaybillReceiptManager {
    @Autowired
    private WaybillReceiptDao waybillReceiptDao;
    @Autowired
    private WaybillIncomeFeeDao waybillIncomeFeeDao;
    @Autowired
    private ReceiptRelationDao receiptRelationDao;
    @Autowired
    private ForecastSenderDao forecastSenderDao;
    @Autowired
    private ForecastReceiverDao forecastReceiverDao;
    @Autowired
    private ForecastVolumeDao forecastVolumeDao;
    @Autowired
    private ForecastDeclareDao forecastDeclareDao;
    /**
     * 前台-运单查询
     * @param query
     * @return
     */
    @Override
    public PageBean<WaybillReceiptVO> listWaybillReceipt(WaybillReceiptQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = waybillReceiptDao.countWaybillReceipt(query);
            PageBean<WaybillReceiptVO> page = new PageTableBean<WaybillReceiptVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(waybillReceiptDao.listWaybillReceipt(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    /**
     * 前台-运单费用查询
     * @param query
     * @return
     */
    @Override
    public PageBean<WaybillReceiptVO> listWaybillFee(WaybillReceiptQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = waybillReceiptDao.countWaybillReceipt(query);
            PageBean<WaybillReceiptVO> page = new PageTableBean<WaybillReceiptVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                List<WaybillReceiptVO> waybillReceiptVOS = waybillReceiptDao.listWaybillReceipt(query);
                waybillReceiptVOS.forEach(waybillReceiptVO ->{
                    waybillReceiptVO.setAmount(BigDecimal.valueOf(waybillIncomeFeeDao.sumIncomeFee(waybillReceiptVO.getWaybillCode())));
                });
                page.addAll(waybillReceiptVOS);
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }

    @Override
    public WaybillDetailVO selectByCustomerNo(WaybillReceiptQuery query){
        WaybillDetailVO waybillDetailVO = new WaybillDetailVO();
        WaybillReceiptVO waybillReceiptVO = waybillReceiptDao.selectWaybillByCustomerNo(query);
        if(Objects.isNull(waybillReceiptVO)){
            log.error("运单信息不存在");
            throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
        }
        waybillDetailVO.setWaybillReceiptVO(waybillReceiptVO);
        ReceiptRelationVO receiptRelationVO = receiptRelationDao.selectByWaybillCode(waybillReceiptVO.getWaybillCode());
        if(Objects.isNull(receiptRelationVO)){
            log.error("运单关系信息不存在");
            throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
        }
        ForecastSenderVO forecastSenderVO = forecastSenderDao.selectByCode(receiptRelationVO.getForecastCode());
        if(Objects.isNull(forecastSenderVO)){
            log.error("运单发货人信息不存在");
            throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
        }
        waybillDetailVO.setForecastSenderVO(forecastSenderVO);

        ForecastReceiverVO forecastReceiverVO = forecastReceiverDao.selectByCode(receiptRelationVO.getForecastCode());
        if(Objects.isNull(forecastReceiverVO)){
            log.error("运单收货人信息不存在");
            throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
        }
        waybillDetailVO.setForecastReceiverVO(forecastReceiverVO);

        List<ForecastVolumeVO> forecastVolumeVOS = forecastVolumeDao.selectByCode(receiptRelationVO.getForecastCode());
        if(CollectionUtil.isEmpty(forecastVolumeVOS)){
            log.error("运单材积信息不存在");
            throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
        }
        waybillDetailVO.setForecastVolumeVOList(forecastVolumeVOS);

        List<ForecastDeclareVO> forecastDeclareVOS = forecastDeclareDao.selectByCode(receiptRelationVO.getForecastCode());
        if(CollectionUtil.isEmpty(forecastDeclareVOS)){
            log.error("运单报关信息不存在");
            throw new TmsManagerException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
        }
        waybillDetailVO.setForecastDeclareVOList(forecastDeclareVOS);
        return  waybillDetailVO;
    }

}
